package GameLogic
{
	import flash.display.Sprite;
	import flash.system.System;
 
    import flash.utils.Timer;
   
    import flash.events.Event;
   
    import flash.events.TimerEvent;
   
    import flash.text.TextField;

	public class FpsCounter extends Sprite
	{
		private var txt:TextField;
 
        private var count:int = 0;

		public function FpsCounter()
		{
			init();
		}
		
		private function init():void
		{
            txt = new TextField();//创建文本实例
			txt.width = 200;
			txt.height = 22;
            txt.textColor = 0xffffff;//设置文本颜色
			txt.scaleX = 1.2;
			txt.scaleY = 1.2;
            addChild(txt);//加载这个文本
            var myTimer:Timer = new Timer(1000);//Timer类挺好使，类似于setInterval,参数是循环间隔时间，单位是毫秒
            myTimer.addEventListener("timer", timerHandler);//注册事件
            this.addEventListener("enterFrame", countHandler);//注册事件，这里相当于2.0的onEnterFrame
            myTimer.start();//Timer实例需要start来进行启动
			txt.selectable = false;
        }
        
		private function timerHandler(event:TimerEvent):void
		{
			//Timer实例调用的方法
             txt.text = "FPS:" + count +  "     Memory:" +int((System.totalMemory/1024)/1000) +"M";
             count = 0;//每隔1秒进行清零
         }
        
		 private function countHandler(event:Event):void
		 {//真循环调用的方法
            count++;//数值递加
         }
 
	}
}